﻿/*
 * Trivial class to provide method to turn exception into string, including inner exception info
 *
 * Edited: 2012-10-08, Perry Rapp
 **/
using System;
using System.Text;

namespace Perry.ExceptionUtility
{
    static class ExceptionUtil
    {
        public static string CombinePrefixAndException(string msg, Exception exc)
        {
            StringBuilder output = new StringBuilder(msg);

            if (msg.EndsWith(" ") || msg.EndsWith(": ")) { /* fine */ }
            else if (msg.EndsWith(":")) output.Append(" ");
            else output.Append(": ");

            output.Append(ExceptionUtil.DescribeException(exc));
            return output.ToString();
        }

        public static string DescribeException(Exception exc)
        {
            return DescribeException(exc, " =++= ");
        }
        public static string DescribeException(Exception exc, string delim)
        {
            string text = exc.Message;
            while (exc.InnerException != null)
            {
                exc = exc.InnerException;
                text += delim + exc.Message;
            }
            return text;
        }
    }
}
